/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import wisp.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author nezie
 */
public class CurseRequest {
    private Random randomizer = new Random(System.currentTimeMillis());
    private String cdoer, creceiver, canswer, cdate, cmonster, cbattleStat;
    private int cid;
    private dbConnection dbcon = new dbConnection();
    
    public int generateId ()
    {
         int num = 1;
        while (checkExist(num))
        {
            Random rand = new Random();
            num = rand.nextInt(100);
            System.out.println(num);
            
        }
        return num;
    }
  
    public CurseRequest() {
    }
     
    public CurseRequest (String doer, String receiver, String answer, String date, int id, String monster, String bStat) {
        cdoer = doer;
        creceiver = receiver;
        canswer = answer;
        cdate = date;
        cid = id;
        cmonster = monster;
        cbattleStat = bStat;
   }

    public String getCdoer() {
        return cdoer;
    }

    public void setCdoer(String cdoer) {
        this.cdoer = cdoer;
    }

    public String getCreceiver() {
        return creceiver;
    }

    public void setCreceiver(String creceiver) {
        this.creceiver = creceiver;
    }

    public String getCanswer() {
        return canswer;
    }

    public void setCanswer(String canswer) {
        this.canswer = canswer;
    }

    public boolean checkExist(int num) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT * FROM CurseRequest WHERE cid = ?";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, num);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              exist = true;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    
   public void addCRequest (CurseRequest c){
      						
        String query; 					
        PreparedStatement stmt;
        c.cid = this.generateId();
        
        if (!this.checkExist(c.cid))
        {
            getDbcon().connect();
            query = "INSERT INTO CurseRequest (cdoer, creceiver, canswer, cdate, cid, cmonster, cBattleStat) VALUES (?,?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, c.getCdoer());
                stmt.setString(2, c.getCreceiver());
                stmt.setString(3, c.canswer);
                stmt.setString(4, CurrentDT.now());
                stmt.setInt(5, c.cid);
                stmt.setString(6, c.cmonster);
                stmt.setString(7, c.cbattleStat);
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            getDbcon().disconnect();               
        }
    }
    public Vector viewCRequest (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<CurseRequest> cList;
            
        cList = new Vector<CurseRequest>();
        Iterator it = cList.iterator();
                
        getDbcon().connect();
        query = "SELECT * FROM CurseRequest WHERE creceiver = ? AND canswer = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            stmt.setString(2, "unknown");
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
            	
                CurseRequest c = new CurseRequest();
                c.canswer = rs.getString("canswer").trim();
                c.cdoer =rs.getString("cdoer").trim();
                c.creceiver = rs.getString("creceiver").trim();
                c.cid = rs.getInt("cid");
                c.cdate = rs.getString("cdate").trim();
                c.cmonster = rs.getString("cmonster");
                c.cbattleStat = rs.getString("cBattleStat");
                cList.add(c);
                System.out.println(c.cid);
           
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return cList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }    
    /* public void deleteCRequest (String doer, String receiver){
         		
    	String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM CurseRequest WHERE cdoer = ? AND creceiver = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, doer);
            stmt.setString(2, receiver);
            stmt.executeUpdate();				
            stmt.close();						
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
     }*/
    
  public void editCRequest (String answer, String date, int id){
        
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE CurseRequest SET canswer = ? WHERE cid = ?";
        try 
        {
             
            stmt = getDbcon().con.prepareStatement(query); 
            stmt.setString (1, answer);
            stmt.setInt(2, id);
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }
   public void viewCRequest (int id){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT * FROM CurseRequest WHERE cid = ? AND canswer = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt (1, id);
            stmt.setString (2, "unknown");
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                this.canswer = rs.getString("canswer").trim();
                this.cdoer =rs.getString("cdoer").trim();
                this.creceiver = rs.getString("creceiver").trim();
                this.cid = rs.getInt("cid");
                this.cdate = rs.getString("cdate").trim();
                this.cmonster = rs.getString("cmonster").trim();                
                this.cbattleStat = rs.getString("cBattleStat").trim();
             }
            //crset.populate(rs);
            rs.close(); 					// close the result
            stmt.close();
           // return crset; // close the statement					// close the statement
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
        }
     }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getCdate() {
        return cdate;
    }

    public void setCdate(String cdate) {
        this.cdate = cdate;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getCmonster() {
        return cmonster;
    }

    public void setCmonster(String cmonster) {
        this.cmonster = cmonster;
    }

    public String getCbattleStat() {
        return cbattleStat;
    }

    public void setCbattleStat(String cbattleStat) {
        this.cbattleStat = cbattleStat;
    }
}
